/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	Administrator 
 * 
 * <p>描   述  :   PlanAction.java for com.pengtu.web.app    </p>
 * 
 * <p>最后修改 : $: 2011-7-27-下午03:14:50 v 1.0.0	 Administrator   $     </p>
 * 
*/
package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Plan;
import com.pengtu.entity.app.User;
import com.pengtu.entity.app.WorkTask;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.app.WorkManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 
 * 我的计划管理Action.T
 * 
 * 2011-7-27 下午03:14:50
 * 
 * @version 1.0.0
 * 
 */
@Namespace("/app")
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "plan.action", type = "redirect") })
public class PlanAction extends BaseActionSupport<Plan>{
	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;
	private WorkManager workManager;
	private SystemManager systemManager;
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	@Autowired
	public void setWorkManager(WorkManager workManager) {
		this.workManager = workManager;
	}

	// -- 页面属性 --//
	private String id;
	private Plan plan;
	private Page<Plan> page = new Page<Plan>(10);// 每页10条记录
	private User user;
	private Attachment attachmention;

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public User getUser() {
		return user;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public Page<Plan> getPage() {
		return page;
	}

	public void setPage(Page<Plan> page) {
		this.page = page;
	}

	// -- ModelDriven 与 Preparable函数 --//
	@Override
	public Plan getModel() {
		return plan;
	}

	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = workManager.getPlanList(page, queryFilter,SpringSecurityUtils.getCurrentUserName());
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		user = SpringSecurityUtils.getCurrentUser();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		/** 保存计划*/
		workManager.savePlan(plan);
		addActionMessage("Title为" + plan.getTitle() + "的计划添加成功Q@!");
		/** 判断是否有上传附件，如果有，上传附件 **/
		if (null != attachmention) {
			attachmention.setTableId(plan.getId());
			attachmention.setTableField("Plan_attachment");
			attachmention.setTableName("Plan");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			attachmention.setCreateDate(DateUtils.getCurrentDate());
			plan.setAttachment(attachmention);
			workManager.savePlan(plan);
			addActionMessage("Title为" + plan.getTitle() + "的计划添加附件名为"
					+ attachmention.getUpfileFileName() + "成功Q@!");
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		addActionMessage("Id" + id + "的计划被删除Q@!");
		workManager.deletePlan(id);
		return RELOAD;
	}
	
	
	/**
	 * 将计划表转化成json形式
	 * */
	public String viewJson() throws Exception {
		/**获取全部计划*/
		String root = Struts2Utils.getRootPath();
		List<Plan> planlist = workManager.getPlans((String)SpringSecurityUtils.getCurrentUser().getUsername());
		List<Map<String, Object>> mapPlanList = new ArrayList<Map<String, Object>>();
		for(Plan plan: planlist){
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("title", plan.getTitle());
			p.put("start", DateUtils.convertDateToDayString(plan.getPlanstarDate()));
			p.put("end", DateUtils.convertDateToDayString(plan.getPlanendDate()));
			p.put("id", plan.getId());
			p.put("color", com.pengtu.utils.StringUtils.colorMapById(plan.getId()));
			p.put("url",root+"/app/plan!input.action?id="+plan.getId());
			p.put("allDay", false);
			mapPlanList.add(p);
		}
		Struts2Utils.renderJson(mapPlanList);
		return NONE;
	}

	public String view() throws Exception {
		return VIEW;
	}
	
	
	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			plan = workManager.getPlan(id);
		} else {
			plan = new Plan();
		}
	}
	
	/**
	 * 
	 * palnAndWork:
	 * 适用:计划和任务整合用到的json
	 * @return json类型
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	
	public String palnAndWork() throws Exception{
		/**获取全部计划*/
		String root = Struts2Utils.getRootPath();
		List<Plan> planlist = workManager.getPlans((String)SpringSecurityUtils.getCurrentUserName());
		List<Map<String, Object>> mapPlanList = new ArrayList<Map<String, Object>>();
		for(Plan plan: planlist){
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("con", "我的计划日程");
			p.put("title", plan.getTitle());
			p.put("start", DateUtils.convertDateToDayString(plan.getPlanstarDate()));
			p.put("end", DateUtils.convertDateToDayString(plan.getPlanendDate()));
			p.put("id", plan.getId());
			p.put("color", com.pengtu.utils.StringUtils.colorMapById(plan.getId()));
			p.put("url",root+"/app/plan!input.action?id="+plan.getId());
			p.put("allDay", false);
			mapPlanList.add(p);
		}
		/**获取我的全部任务*/
		List<WorkTask> worklist = workManager.getWorktaskList(SpringSecurityUtils.getCurrentUser().getUsername());
		for(WorkTask work: worklist){
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("con", "我的任务日程");
			p.put("title", work.getTitle());
			p.put("start", DateUtils.convertDateToDayString(work.getPlanStarDate()));
			p.put("end", DateUtils.convertDateToDayString(work.getPlanEndDate()));
			p.put("id", work.getId());
			p.put("color", com.pengtu.utils.StringUtils.colorMapById(work.getId()));
			p.put("url",root+"/app/worktask!input.action?id="+work.getId());
			p.put("allDay", false);
			mapPlanList.add(p);
		}
		Struts2Utils.renderJson(mapPlanList);
		return NONE;
	}
}
